/**
 * ━━━━━━神兽出没━━━━━━
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑, 永无BUG!
 * 　　　　┃　　　┃Code is far away from bug with the animal protecting
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━感觉萌萌哒━━━━━━
 */
package com.zkgengkun.console.service.impl;

import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.api.SnsAccessToken;
import com.jfinal.weixin.sdk.api.SnsAccessTokenApi;
import com.jfinal.weixin.sdk.api.SnsApi;
import com.zkgengkun.console.service.WeChatService;
import com.zkgengkun.constant.SysConstants;
import com.zkgengkun.support.util.Md5Utils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author gengshikun
 * @date 2017/3/2
 */
@Service("weChatService")
public class WeChatServiceImpl implements WeChatService {

    private static final Logger logger = LoggerFactory.getLogger(WeChatServiceImpl.class);

    @Value("${wx.appid}")
    private String appId;

    @Value("${wx.secret}")
    private String secret;

    @Value("${wx.host}")
    private String host;

    @Value("${wx.name}")
    private String name;

    /**
     * 获得微信Code
     *
     * @param response
     */
    @Override
    public void getCode(HttpServletResponse response) {
        String authorizeUrl = SnsAccessTokenApi.getAuthorizeURL(appId, host + "/get_access_token", Md5Utils.MD5(name, SysConstants.WE_CHAT_STATE_KEY, "UTF-8"), Boolean.FALSE);
        logger.info(">>>>>>>>>>>>>>>>>>>>>>>>>> authorizeUrl: {}", authorizeUrl);
        try {
            response.sendRedirect(authorizeUrl);
        } catch (IOException e) {
            e.printStackTrace();
            logger.error(">>>>>>>>>>>>>>>>>>>>>>>>>> send authorizeUrl error: {}", authorizeUrl);
        }

    }

    /**
     * 获得微信AccessToken
     *
     * @param request
     * @param response
     */
    @Override
    public void getAccessToken(HttpServletRequest request, HttpServletResponse response) {
        String code = request.getParameter("code");
        String state = request.getParameter("state");
        if (StringUtils.isNotBlank(code) && StringUtils.isNotBlank(state)) {
            // 验证state
            logger.info(">>>>>>>>>>>>>>>>>> state: {}", state);
            if (Md5Utils.verify(name, state, SysConstants.WE_CHAT_STATE_KEY, "UTF-8")) {
                SnsAccessToken accessToken = SnsAccessTokenApi.getSnsAccessToken(appId, secret, code);
                if (accessToken != null) {
                    ApiResult result = SnsApi.getUserInfo(accessToken.getAccessToken(), accessToken.getOpenid());
                    if (result.isSucceed()) {
                        logger.info(">>>>>>>>>>>>>>>>>> wxUser: {}", result.getJson());
                    }
                }
            }
        }
    }
}
